﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;

namespace Draco.Commons.Lang
{
    /// <summary>
    /// 数组辅助类
    /// </summary>
    public class ArrayHelper
    {
        /// <summary>
        /// Int64数组转换为Int32数组
        /// </summary>
        /// <param name="Int64Arr"></param>
        /// <returns></returns>
        public static int[] Int64ArrayToInt(Int64[] Int64Arr)
        {
            if (Int64Arr == null)
                return null;
            int[] ret = new int[Int64Arr.Length];
            for (int i = 0; i < Int64Arr.Length; i++)
            {
                ret[i] = (int)Int64Arr[i];
            }
            return ret;
        }
        /// <summary>
        /// Int32数组转换为Int64数组
        /// </summary>
        /// <param name="intArr"></param>
        /// <returns></returns>
        public static Int64[] IntArrayToInt64(int[] intArr)
        {
            if (intArr == null)
                return null;
            Int64[] ret = new Int64[intArr.Length];
            for (int i = 0; i < intArr.Length; i++)
            {
                ret[i] = intArr[i];
            }
            return ret;
        }

        /// <summary>
        /// 字符串数组转换为Int数组
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        public static int[] StringArrayToIntArray(string[] strs)
        {
            if (strs == null)
                return null;
            int[] ret = new int[strs.Length];
            for (int i = 0; i < ret.Length; i++)
            {
                int x = 0;
                int.TryParse(strs[i], out x);
                ret[i] = x;
            }
            return ret;
        }

        /// <summary>
        /// int数组转换为字符串数组
        /// </summary>
        /// <param name="IntArr"></param>
        /// <returns></returns>
        public static string[] IntArrayToStringArray(int[] IntArr)
        {
            if (IntArr == null)
                return null;
            string[] ret = new string[IntArr.Length];
            for (int i = 0; i < ret.Length; i++)
            {
                ret[i] = IntArr[i].ToString();
            }
            return ret;
        }

        /// <summary>
        /// 把数组转化成字符串
        /// </summary>
        /// <param name="array"></param>
        /// <param name="splitChr"></param>
        /// <returns></returns>
        public static string ArrayToString(Array array, char splitChr)
        {
            if (array == null || array.Length == 0)
                return "";
            StringBuilder rtn = new StringBuilder();
            for (int i = 0; i < array.Length; i++)
            {
                object str = array.GetValue(i);
                if (str != null)
                    rtn.Append(str.ToString());

                rtn.Append(splitChr);
            }
            string Str = rtn.ToString();
            int tmp = Str.LastIndexOf(splitChr);
            if (tmp != -1)
                Str = Str.Substring(0, tmp);
            return Str;
        }

        /// <summary>
        /// 删除数组中的重复项
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static Array RemoveRepeatItem(Array array)
        {
            if (array != null && array.Length > 0)
            {
                ArrayList list = new ArrayList();
                foreach (object o in array)
                {
                    if (!list.Contains(o))
                        list.Add(o);
                }
                return list.ToArray();
            }
            return array;
        }

        /// <summary>
        /// 用数据集的列数据填充数组
        /// </summary>
        /// <param name="dataSet">数据集</param>
        /// <param name="FieldName">字段名称</param>
        /// <returns></returns>
        public static String[] FillFieldToArray(DataSet dataSet,string FieldName)
        {
            if (dataSet != null && dataSet.Tables.Count > 0)
            {
                return FillFieldToArray(dataSet.Tables[0], FieldName);
            }
            return new string[0];
        }
        /// <summary>
        /// 用数据表的列数据填充数组
        /// </summary>
        /// <param name="dataTable">数据表</param>
        /// <param name="FieldName">字段名称</param>
        /// <returns></returns>
        public static String[] FillFieldToArray(DataTable dataTable,string FieldName)
        {
            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                if (String.IsNullOrEmpty(FieldName) || dataTable.Columns[FieldName] == null)
                    throw new ArgumentException("FieldName error!");
                string[] array = new string[dataTable.Rows.Count];
                int i = 0;
                foreach (DataRow row in dataTable.Rows)
                {
                    array[i] = Convert.ToString(row[FieldName]);
                    i++;
                }
                return array;
            }
            return new string[0];
        }
        /// <summary>
        /// 从字符串数组中查找某项
        /// </summary>
        /// <param name="strs"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static int FindInArray(Array strs, string val)
        {
            if (strs == null || strs.Length == 0)
                return -1;

            for (int i = 0; i < strs.Length; i++)
            {
                object str = strs.GetValue(i);
                if (str != null)
                {
                    if (str.ToString() == val)
                        return i;
                }
                else if (val == null)
                {
                    return i;
                }
            }
            return -1;
        }

        /// <summary>
        /// 从字符串数组中删除某项
        /// </summary>
        /// <param name="ary"></param>
        /// <param name="val"></param>
        /// <returns></returns>
        public static string[] DeleteFromArray(Array ary, string val)
        {
            if (ary == null)
                return null;
            else if (ary.Length == 0)
                return new string[0];
            ArrayList lst = new ArrayList();
            lock (ary.SyncRoot)
            {
                for (int i = 0; i < ary.Length; i++)
                {
                    object obj = ary.GetValue(i);
                    if (obj == null && val == null)
                        continue;
                    if (obj.ToString() != val)
                        lst.Add(obj.ToString());
                }
            }
            return (string[])lst.ToArray(typeof(string));
        }

        /// <summary>
        /// 把数组转换为字符串，以逗号分割，每一项添加单引号(可用于SQL的集合查询)
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static string ArrayToStringWithQuotation(Array array)
        {
            string Quotation = "'";
            char splitChr = ',';
            if (array == null || array.Length == 0)
                return "";
            StringBuilder rtn = new StringBuilder();
            rtn.Append(Quotation);      //第一个引号
            for (int i = 0; i < array.Length; i++)
            {
                object str = array.GetValue(i);
                if (str != null)
                    rtn.Append(str.ToString());
                rtn.Append(Quotation);  //第二个引号
                rtn.Append(splitChr);
                rtn.Append(Quotation);  //下一个引号
            }
            string Str = rtn.ToString();
            int tmp = Str.LastIndexOf(splitChr);
            if (tmp != -1)
                Str = Str.Substring(0, tmp);
            return Str;
        }

        /// <summary>
        /// 把数组转化成字符串
        /// </summary>
        /// <param name="list"></param>
        /// <param name="splitChr"></param>
        /// <returns></returns>
        public static string ArrayToString(IList list, char splitChr)
        {
            StringBuilder rtn = new StringBuilder();
            if (list == null)
                return string.Empty;
            for (int i = 0; i < list.Count; i++)
            {
                rtn.Append(list[i].ToString());
                rtn.Append(splitChr);
            }
            string str = rtn.ToString();
            int tmp = str.LastIndexOf(splitChr);
            if (tmp != -1)
                str = str.Substring(0, tmp);
            return str;
        }
    }
}
